package algotithm.leetcode.dichotomy.test1337;

import java.util.Arrays;

/**
 * @author zhouyanxiang
 * @Date 2021-01-2021/1/8-14:40
 */
public class Solution {

    public static void main(String[] args) {
        Solution solution = new Solution();
        int[][] mat = {{1,1,0,0,0},{1,1,1,1,0},{1,0,0,0,0},{1,1,0,0,0},{1,1,1,1,1}};
        int k = 3;
        int[] rows = solution.kWeakestRows(mat, k);
        for (int i = 0; i < rows.length; i++) {
            System.out.print(rows[i] + " ");
        }
    }

    public int[] kWeakestRows(int[][] mat, int k) {
        int[] arr = new int[mat.length];
        int[] res = new int[k];
        for (int i = 0; i < mat.length; i++) {
            for (int j = 0; j < mat[0].length; j++) {
                arr[i] = count(mat[i]) * 1000 + i;
            }
        }
        Arrays.sort(arr);
        for (int i = 0; i < k; i++) {
            res[i] = arr[i] % 1000;
        }
        return res;
    }

    public int count(int[] num) {
        int res = 0;
        for (int i = 0; i < num.length; i++) {
            if (num[i] != 1) {
                break;
            }
            res += num[i];
        }
        return res;
    }

}
